package com.silent.stest;

import java.io.*;
import java.net.*;

public class SocketClient {
	static Socket client;
	
	public SocketClient(String site, int port){
		try{
			client = new Socket(site,port);
			System.out.println("Test client is created! Server site: "+site+"  Port: "+port);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public boolean sendMsg(String msg){
		try{
			if (msg.isEmpty() || msg.equals("quit")) {
				//BufferedReader msgin = new BufferedReader(new InputStreamReader(client.getInputStream()));
				PrintWriter out = new PrintWriter(client.getOutputStream());
				out.println(msg);
				out.flush();
				return false;
			} else{
				PrintWriter out = new PrintWriter(client.getOutputStream());
				out.println(msg);
				out.flush();
				return true;
			}
		} catch (IOException e){
			e.printStackTrace();
			return false;
		}
	}
	
	public void close(){
        try{
            if(client != null) client.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
